﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using bzxxxt.admin.entity;
using bzxxxt.admin.service;
using bzxxxt.bll;
using bzxxxt.dal;
using bzxxxt.model;

namespace bzxxxt.admin.ashx
{
    /// <summary>
    /// bg_paradise 的摘要说明
    /// </summary>
    public class bg_paradise : admin.ashx.rm_base
    {

        ParadiseBLL paradiseBLL = new ParadiseBLL();
        public override void add(HttpContext context)
        {
            string id = context.Request.Params["ui_paradise_id"] ?? "";
            string name = context.Request.Params["ui_paradise_name"] ?? "";
            string type = context.Request.Params["ui_paradise_type"] ?? "";
            string areacode = context.Request.Params["ui_paradise_areaId"] ?? "";
            string fullName = context.Request.Params["ui_paradise_fullName"] ?? "";
            string photo = context.Request.Params["ui_paradise_pic"] ?? "";

            string paradiseNum = context.Request.Params["ui_paradise_num"] ?? "1";
            string graves = context.Request.Params["ui_paradise_graves"] ?? "0";
            //string floors = context.Request.Params["ui_paradise_floors"] ?? "0";
            //string eastSections = context.Request.Params["ui_paradise_eastSections"] ?? "0";
            //string westSections = context.Request.Params["ui_paradise_westSections"] ?? "0";
            //string lines = context.Request.Params["ui_paradise_lines"] ?? "0";
            //string layers = context.Request.Params["ui_paradise_layers"] ?? "0";
            //string columns = context.Request.Params["ui_paradise_columns"] ?? "0";
            //string graves = context.Request.Params["ui_paradise_graves"] ?? "0";
            string fileName = context.Server.MapPath(photo);
            string code = paradiseBLL.GetCodeByAreaId(areacode);
            string photo2 = "/admin/images/" + code + Path.GetExtension(fileName);
            if (photo.IndexOf("paradise.jpg") < 0 && photo != photo2)
            {
                //修改了图片
                string photoFile = context.Server.MapPath("/") + photo2;
                if (File.Exists(photoFile))
                {
                    File.Delete(photoFile);
                }
                File.Move(fileName, photoFile);
            }
            else
            {
                photo2 = photo;
            }

            tbParadise paradise = new tbParadise
            {
                name = name,
                fullName = fullName,
                pic = photo2,
                crdate = DateTime.Now,
                areacode = areacode,
                code = code + paradiseNum,
                type = type,
                paradiseNum = int.Parse(paradiseNum),
                graves = int.Parse(graves)
                //floors = int.Parse(floors),
                //westSections = int.Parse(westSections),
                //eastSections = int.Parse(eastSections),
                //lines = int.Parse(lines),
                //layers = int.Parse(layers),
                //columns = int.Parse(columns),
                //graves = int.Parse(graves)
            };

            context.Response.Write(paradiseBLL.Add(paradise));
        }

        public override void edit(HttpContext context)
        {
            string id = context.Request.Params["ui_paradise_id"] ?? "";
            string name = context.Request.Params["ui_paradise_name"] ?? "";
            string type = context.Request.Params["ui_paradise_type"] ?? "";
            string areacode = context.Request.Params["ui_paradise_areaId"] ?? "";
            
            string fullName = context.Request.Params["ui_paradise_fullName"] ?? "";
            string paradiseNum = context.Request.Params["ui_paradise_num"] ?? "1";
            string photo = context.Request.Params["ui_paradise_pic"] ?? "";
            string graves = context.Request.Params["ui_paradise_graves"] ?? "0";
            string fileName = context.Server.MapPath(photo);
            string code = paradiseBLL.GetCodeByAreaId(areacode);
            string photo2 = "/admin/images/" + code + Path.GetExtension(fileName);
            if (photo.IndexOf("paradise.jpg") < 0 && photo != photo2)
            {
                //修改了图片
                string photoFile = context.Server.MapPath("/") + photo2;
                if (File.Exists(photoFile))
                {
                    File.Delete(photoFile);
                }
                File.Move(fileName, photoFile);
            }
            else
            {
                photo2 = photo;
            }


            tbParadise paradise = new tbParadise
            {
                id = int.Parse(id),
                code = code + paradiseNum,
                name = name,
                type = type,
                fullName = fullName,
                pic = photo2,
                areacode = areacode,
                paradiseNum = int.Parse(paradiseNum),
                graves = int.Parse(graves)
            };

            context.Response.Write(paradiseBLL.Edit(paradise));
        }

        public override void delete(HttpContext context)
        {
            string ids = context.Request.Params["id"];
            context.Response.Write(paradiseBLL.Delete(ids));
        }

        public override void getall(HttpContext context)
        {
            context.Response.Write(paradiseBLL.GetAll());
        }

        public override void search(HttpContext context)
        {
            string where = context.Request.Params["where"] ?? "1=1";
            int pageIndex = CommUtil.GetPageIndex(context);
            int pageSize = CommUtil.GetPageSize(context);
            string mySort = CommUtil.GetSort(context) + " " + CommUtil.GetOrder(context);

            context.Response.Write(paradiseBLL.GetListByPage(pageSize, pageIndex, mySort, where));

        }
        public override void other(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["action"];

            switch (action)
            {
                case "getusers":
                    {
                        context.Response.Write(paradiseBLL.getUserList());
                        break;
                    }
                case "import":
                    {
                        string op = context.Request.Params["op"];
                        if (op == "upload")
                        {
                            UserEntity user = UserService.GetUser(context);
                            //文件名  
                            HttpPostedFile f1 = context.Request.Files["ui_paradise_import_file"];

                            string fileExt = System.IO.Path.GetExtension(f1.FileName);
                            if (!(fileExt == ".xls" || fileExt == ".xlsx"))
                            {
                                context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "只支持扩展名为.xls或.xlsx文件！" }));
                                return;
                            }
                            string fileName = user.userName + "_paradise_" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
                            f1.SaveAs(context.Server.MapPath("~/admin/upload/") + fileName);
                            context.Response.Write(JsonUtil.ToJson(new { success = true, msg = fileName }));
                        }
                        else if (op == "import")
                        {
                            string file = context.Request.Params["ui_paradise_import_filename"];
                            string excludes = context.Request.Params["ui_paradise_import_exclude"];
                            List<int> exs = new List<int>();
                            if (!string.IsNullOrEmpty(excludes))
                            {
                                string[] ss = excludes.Split(',');
                                foreach (var s in ss)
                                {
                                    exs.Add(int.Parse(s));
                                }
                            }
                            if (string.IsNullOrEmpty(file))
                            {
                                context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "文件未上传！" }));
                            }
                            else
                            {
                                file = context.Server.MapPath("~/admin/upload/") + file;
                                try
                                {
                                    List<ParadiseLayoutModel> list = paradiseBLL.ImportExceltoDb(file, 0, 1);
                                    paradiseBLL.Import(list,exs);
                                    context.Response.Write(JsonUtil.ToJson(new { success = true, msg = "导入成功！" }));
                                }
                                catch (Exception ex)
                                {

                                    context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "导入失败: " + ex.Message }));
                                }
                            }
                        }
                        break;
                    }
                case "init":
                    {
                        string str = context.Request.Params["ui_paradise_id"] ?? "";
                        int pid = int.Parse(str);
                        string hourseNum = context.Request.Params["ui_paradise_hourseNum"] ?? "";
                        string floorNum = context.Request.Params["ui_paradise_floorNum"] ?? "0";
                        string sectionNum = context.Request.Params["ui_paradise_sectionNum"] ?? "0";
                        string lines = context.Request.Params["ui_paradise_lines"] ?? "0";
                        string layers = context.Request.Params["ui_paradise_layers"] ?? "0";
                        string columns = context.Request.Params["ui_paradise_columns"] ?? "0";
                        string graveType = context.Request.Params["ui_paradise_grave_type"];
                        string excludes = context.Request.Params["ui_paradise_exclude"];
                        string startNum = context.Request.Params["ui_paradise_startNum"];   //起始编号
                        string endNum = context.Request.Params["ui_paradise_endNum"];       //末位数字
                        int sNum = int.Parse(startNum);
                        int eNum = int.Parse(endNum);
                        List<int> exs = new List<int>();
                        if (!string.IsNullOrEmpty(excludes)) {
                            string[] ss = excludes.Split(',');
                            foreach (var s in ss) {
                                exs.Add(int.Parse(s));
                            }
                        }
                        if (string.IsNullOrEmpty(graveType)) graveType = "1";
                        int gtype = int.Parse(graveType);
                        ParadiseLayoutModel p = new ParadiseLayoutModel();
                        p.gtype = gtype;
                        p.id = pid;
                        p.dong = int.Parse(hourseNum);
                        p.floor = int.Parse(floorNum);
                        p.section = int.Parse(sectionNum);
                        p.lines = lines;                        
                        p.layers = int.Parse(layers);
                        p.columns = int.Parse(columns);
                        p.startIndex = sNum;
                        p.endNum = eNum;
                        
                        context.Response.Write(paradiseBLL.Init(p,exs));
                        break;
                    }

                case "setadmin":
                    {
                        string paradiseId = context.Request.Params["paradiseId"];
                        string userIds = context.Request.Params["userIds"];
                        context.Response.Write(paradiseBLL.SetAdmin(int.Parse(paradiseId), userIds));
                        break;
                    }

            }

        }

    }
}